package com.zlx.mall.basic.util.text;

import org.springframework.lang.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Expand org.springframework.util.StringUtils
 *
 * @author qingke
 * @see org.springframework.util.StringUtils
 **/
public class MoreStringUtil {

    private static final List<String> EMPTY_STRING_LIST= new ArrayList<>();

    /**
     * ASCII a
     */
    private static final int ASCII_LOW_CASE_MIN = 97;
    /**
     * ASCII z
     */
    private static final int ASCII_LOW_CASE_MAX = 122;
    /**
     * Move
     */
    private static final int ASCII_MOVE = 32;

    /**
     * Initial Capital
     * <a href="https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html">ASCII Table</a>
     * @param s s
     * @return letter
     */
    private static String initialCapital(String s) {
        char[] chars = s.toCharArray();
        if (ASCII_LOW_CASE_MIN <= chars[0] && chars[0] <= ASCII_LOW_CASE_MAX) {
            chars[0] -= ASCII_MOVE;
        }
        return String.valueOf(chars);
    }

    /**
     * Tokenize the given {@code String} into a {@code String} list via a
     * {@link StringTokenizer}.
     * <p>Trims tokens and omits empty tokens.
     * <p>The given {@code delimiters} string can consist of any number of
     * delimiter characters. Each of those characters can be used to separate
     * tokens. A delimiter is always a single character; for multi-character
     * delimiters, consider using {@link org.springframework.util.StringUtils#delimitedListToStringArray}.
     * @param str the {@code String} to tokenize (potentially {@code null} or empty)
     * @param delimiters the delimiter characters, assembled as a {@code String}
     * (each of the characters is individually considered as a delimiter)
     * @return a list of the tokens
     * @see org.springframework.util.StringUtils#tokenizeToStringArray(String, String)
     * @see StringTokenizer
     * @see String#trim()
     * @see org.springframework.util.StringUtils#delimitedListToStringArray
     */
    public static List<String> tokenizeToStringList(@Nullable String str, String delimiters) {
        return tokenizeToStringList(str, delimiters, true, true);
    }

    /**
     * Tokenize the given {@code String} into a {@code String} list via a
     * {@link StringTokenizer}.
     * <p>The given {@code delimiters} string can consist of any number of
     * delimiter characters. Each of those characters can be used to separate
     * tokens. A delimiter is always a single character; for multi-character
     * delimiters, consider using {@link org.springframework.util.StringUtils#delimitedListToStringArray}.
     * @param str the {@code String} to tokenize (potentially {@code null} or empty)
     * @param delimiters the delimiter characters, assembled as a {@code String}
     * (each of the characters is individually considered as a delimiter)
     * @param trimTokens trim the tokens via {@link String#trim()}
     * @param ignoreEmptyTokens omit empty tokens from the result array
     * (only applies to tokens that are empty after trimming; StringTokenizer
     * will not consider subsequent delimiters as token in the first place).
     * @return a list of the tokens
     * @see org.springframework.util.StringUtils#tokenizeToStringArray(String, String, boolean, boolean)
     * @see StringTokenizer
     * @see String#trim()
     * @see org.springframework.util.StringUtils#delimitedListToStringArray
     */
    public static List<String> tokenizeToStringList(
            @Nullable String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) {

        if (str == null) {
            return EMPTY_STRING_LIST;
        }

        StringTokenizer st = new StringTokenizer(str, delimiters);
        List<String> tokens = new ArrayList<>();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (trimTokens) {
                token = token.trim();
            }
            if (!ignoreEmptyTokens || token.length() > 0) {
                tokens.add(token);
            }
        }
        return tokens;
    }


}
